<template>
  <!-- 产品栏目管理 -->
  <div>
    <!-- 头部添加 -->
    <div class="header">
      <el-form :inline="true" :model="params" class="demo-form-inline">
        <el-button type="primary" @click="toAdd">添加</el-button>
      </el-form>
    </div>
    <!-- 头部添加结束 -->
    <!-- 第二行内容 -->
    <el-table :data="columns" style="width: 100%">
      <el-table-column type="index" :index="1" label="序号" width="100" />
      <el-table-column prop="id" label="栏目编号" />
      <el-table-column prop="name" label="名称" />
      <el-table-column prop="icon" label="图标">
        <template slot-scope="scope">
          <el-popover placement="right" trigger="hover">
            <img
              slot="reference"
              :src="scope.row.icon"
              width="30px"
              alt="加载失败"
            >
            <img width="200px" :src="scope.row.icon" alt="">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="num" label="排序号" width="200" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" plain @click="toEdit(scope.row)">编辑</el-button>
          <el-button type="danger" plain @click.prevent="todelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 内容结束 -->
    <!-- 第三行分页 -->
    <div class="page">
      <el-pagination
        :current-page="params.page"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 分页结束 -->
    <!-- 编辑模态框 -->
    <el-dialog :title="title" :visible.sync="visible" @close="toClose">
      <el-form ref="myForm" :rules="rules" :model="form">
        <el-form-item
          label="栏目名称"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input v-model="form.name" autocomplete="off" />        
        </el-form-item>
        <el-form-item label="序号" :label-width="formLabelWidth" prop="num">
          <el-input v-model="form.num" autocomplete="off" />
        </el-form-item>
        <!-- 封面 -->
        <el-form-item label="图标" :label-width="formLabelWidth" prop="icon">
          <el-upload
            class="avatar-uploader"
            :action="uploadFileURL"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="toClose">取 消</el-button>
        <el-button type="primary" @click="toSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑模态框结束 -->
  </div>
</template>

<script>
import { uploadFileURL } from '@/utils/config'
import { pageQuery, deleteColumns,saveColumns } from '@/api/proCategory'
export default {
  data() {
    return {
      title: '',
      // 上传图片变量
      imageUrl: '',
      // 图片上传地址
      uploadFileURL,
      params: {
        page: 1,
        pageSize: 10
        // name:''
      },
      columns: [],
      // productData: []
      total: 0,
      // 控制模态框显示与隐藏
      visible: false,
      // 表单宽度
      form: {},
      // 表单宽度
      formLabelWidth: '120px',
      rules: {
        name: [{ required: true, message: '请输入栏目名称', trigger: 'blur' }],
        icon: [{ required: true, message: '请添加图标', trigger: 'change' }]
      }
    }
  },
  computed: {},
  created() {
    this.pageQuery()
  },
  mounted() {},
  methods: {
    todelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deleteColumns({ id })
        if (res.status === 200) {
          this.$message({
            type: 'success',
            message: res.message

          })
          this.pageQuery()
          this.toClose()
        } else {
          this.$message({
            type: 'info',
            message: res.message
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    toEdit(row) {
      this.title = '修改产品信息'
      this.visible = true
      this.form = { ...row }
      this.imageUrl = this.form.icon
    },
    // 保存或更新操作
    toSubmit() {
      this.$refs.myForm.validate(async(valid) => {
        if (valid) {
          // 发送保存或更新请求
          const res = await saveColumns(this.form)
          this.$notify({
            type: 'success',
            message: res.message
          })
          this.pageQuery()
          this.toClose()
        } else {
          return false
        }
      })
    },
    // 图片上传成功的回调
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.form.icon = res.data
    },
    // 图片上传前回调
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    // 取消模态框
    toClose() {
      setTimeout(() => {
        this.$refs.myForm.resetFields()
        this.visible = false
      }, 300)
    },
    // 页码条数发生改变的方法
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize
      this.pageQuery()
      this.params.page = 1
    },
    // 页码发生改变的方法
    handleCurrentChange(page) {
      this.params.page = page
      this.pageQuery()
    },
    // 添加按钮
    toAdd() {
      this.visible = true
      this.title = '添加产品信息'
      this.form = {}
      this.imageUrl = ''
    },
    async pageQuery() {
      for (const key in this.params) {
        // key是属性名
        if (!this.params[key]) {
          delete this.params[key]
        }
      }
      const res = await pageQuery(this.params)
      // console.log(res);
      this.columns = res.data.list
      this.total = res.data.total
    }
  }
}
</script>
<style scoped lang="scss">
  // .toEdit{
  // color: #30b08f;

  // }
//   .delete{
// color: #e65d6e;
//   }
</style>
